package org.example.service;

import org.springframework.context.annotation.Bean;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


@Service
public class UserDetailServiceImpl implements UserDetailsService {

    @Resource
    private BCryptPasswordEncoder bCryptPasswordEncoder;
    /**
     * 返回我们的用户登录对象
     * @param username the username identifying the user whose data is required.
     * @return  把我们输入的用户名和密码在数据库中操作后，把返回的用户名，密码，权限等内容
     *          全部打包给Spring User对象
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = null;
        if(username.equals("jason")){
             user = new User(username,
                    bCryptPasswordEncoder.encode("123456"),
                    AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_ADMIN") );  //ADMIN
            System.out.println("::::::::"+user);  //PROTECTED
        }
        return user;
    }

}
